package valuecomusa.fijibus.eticket;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Environment;
import android.util.Log;
import flexjson.JSONSerializer;
import org.kobjects.base64.Base64;
import valuecomusa.fijibus.eticket.db.shorthaul.BusinessDBHandler;
import valuecomusa.fijibus.eticket.dto.DataDeleted;
import valuecomusa.fijibus.eticket.ws.*;
import valuecomusa.fijibus.eticket.shorthaul.R;

import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: fireman
 * Timestamp: 5/20/13
 * Time: 7:24 AM
 * To change this template use File | Settings | File Templates.
 */
public class DataCenter
{
    private final String TAG = "DataCenter";

    private Context myContext = null;
    private final int dataPageSize = 20;

    public DataCenter(Context ctx)
    {
        myContext = ctx;
    }

    //---------------------------------------------------------
    // Get data from webservice section
    // for full sync
    //---------------------------------------------------------
    public RestResultValues<CountInfo> conductorCount;
    public RestResultValues<CountInfo> memberCount;
    public RestResultValues<CountInfo> busCount;
    public RestResultValues<CountInfo> routeCount;
    public RestResultValues<CountInfo> routeAssignCount;
    public RestResultValues<CountInfo> stageCount;
    public RestResultValues<CountInfo> busStationCount;
    public RestResultValues<CountInfo> busStopCount;
    public RestResultValues<CountInfo> expressFareCount;
    public RestResultValues<CountInfo> stageFareCount;
    public RestResultValues<CountInfo> busSeatCount;
    public RestResultValues<CountInfo> busAssignCount;
    public RestResultValues<CountInfo> passengerTypeCount;

    public synchronized int getTotalDataCount()
    {
        Log.d(TAG, "[getTotalDataCount] ----------getTotalDataCount---------");
        Log.d(TAG, "[getTotalDataCount] 1. Get conductors count...");
        RESTClient ws = new RESTClient(myContext);
        conductorCount = ws.doGetDataCount(
                myContext.getString(R.string.sunbeam_ws_url) + "/api/request?type=conductor&count"
        );
        if(conductorCount == null || conductorCount.Code != 0) //Error
        {
            return -1;
        }
        Log.d(TAG, "[getTotalDataCount] Total: " + conductorCount.Data.get(0).Count);
        Log.d(TAG, "[getTotalDataCount] 2. Get members count...");
        memberCount = ws.doGetDataCount(
                myContext.getString(R.string.sunbeam_ws_url) + "/api/request?type=member&count"
        );
        if(memberCount == null || memberCount.Code != 0) //Error
        {
            return -1;
        }
        Log.d(TAG, "[getTotalDataCount] Total: " + memberCount.Data.get(0).Count);
        Log.d(TAG, "[getTotalDataCount] 3. Get Bus count...");
        busCount = ws.doGetDataCount(
                myContext.getString(R.string.sunbeam_ws_url) + "/api/request?type=bus&count"
        );
        if(busCount == null || busCount.Code != 0) //Error
        {
            return -1;
        }
        Log.d(TAG, "[getTotalDataCount] Total: " + busCount.Data.get(0).Count);
        Log.d(TAG, "[getTotalDataCount] 4. Get Route count...");
        routeCount = ws.doGetDataCount(
                myContext.getString(R.string.sunbeam_ws_url) + "/api/request?type=route&count"
        );
        if(routeCount == null || routeCount.Code != 0) //Error
        {
            return -1;
        }
        Log.d(TAG, "[getTotalDataCount] Total: " + routeCount.Data.get(0).Count);
        Log.d(TAG, "[getTotalDataCount] 5. Get RouteAssign count...");
        routeAssignCount = ws.doGetDataCount(
                myContext.getString(R.string.sunbeam_ws_url) + "/api/request?type=routeassign&count"
        );
        if(routeAssignCount == null || routeAssignCount.Code != 0) //Error
        {
            return -1;
        }
        Log.d(TAG, "[getTotalDataCount] Total: " + routeAssignCount.Data.get(0).Count);
        Log.d(TAG, "[getTotalDataCount] 6. Get Stage count...");
        stageCount = ws.doGetDataCount(
                myContext.getString(R.string.sunbeam_ws_url) + "/api/request?type=stage&count"
        );
        if(stageCount == null || stageCount.Code != 0) //Error
        {
            return -1;
        }
        Log.d(TAG, "[getTotalDataCount] Total: " + stageCount.Data.get(0).Count);
        Log.d(TAG, "[getTotalDataCount] 7. Get BusStation count...");
        busStationCount = ws.doGetDataCount(
                myContext.getString(R.string.sunbeam_ws_url) + "/api/request?type=busstation&count"
        );
        if(busStationCount == null || busStationCount.Code != 0) //Error
        {
            return -1;
        }
        Log.d(TAG, "[getTotalDataCount] Total: " + busStationCount.Data.get(0).Count);
        Log.d(TAG, "[getTotalDataCount] 8. Get BusStop count...");
        busStopCount = ws.doGetDataCount(
                myContext.getString(R.string.sunbeam_ws_url) + "/api/request?type=busstop&count"
        );
        if(busStopCount == null || busStopCount.Code != 0) //Error
        {
            return -1;
        }
        Log.d(TAG, "[getTotalDataCount] Total: " + busStopCount.Data.get(0).Count);
        Log.d(TAG, "[getTotalDataCount] 9. Get ExpressFare count...");
        expressFareCount = ws.doGetDataCount(
                myContext.getString(R.string.sunbeam_ws_url) + "/api/request?type=expressfare&count"
        );
        if(expressFareCount == null || expressFareCount.Code != 0) //Error
        {
            return -1;
        }
        Log.d(TAG, "[getTotalDataCount] Total: " + expressFareCount.Data.get(0).Count);
        Log.d(TAG, "[getTotalDataCount] 10. Get StageFare count...");
        stageFareCount = ws.doGetDataCount(
                myContext.getString(R.string.sunbeam_ws_url) + "/api/request?type=stagefare&count"
        );
        if(stageFareCount == null || stageFareCount.Code != 0) //Error
        {
            return -1;
        }
        Log.d(TAG, "[getTotalDataCount] Total: " + stageFareCount.Data.get(0).Count);
        Log.d(TAG, "[getTotalDataCount] 11. Get BusSeat count...");
        busSeatCount = ws.doGetDataCount(
                myContext.getString(R.string.sunbeam_ws_url) + "/api/request?type=busseat&count"
        );
        if(busSeatCount == null || busSeatCount.Code != 0) //Error
        {
            return -1;
        }
        Log.d(TAG, "[getTotalDataCount] Total: " + busSeatCount.Data.get(0).Count);
        Log.d(TAG, "[getTotalDataCount] 12. Get BusAssign count...");
        busAssignCount = ws.doGetDataCount(
                myContext.getString(R.string.sunbeam_ws_url) + "/api/request?type=busassign&count"
        );
        if(busAssignCount == null || busAssignCount.Code != 0) //Error
        {
            return -1;
        }
        Log.d(TAG, "[getTotalDataCount] Total: " + busAssignCount.Data.get(0).Count);
        Log.d(TAG, "[getTotalDataCount] 13. Get PassengerType count...");
        passengerTypeCount = ws.doGetDataCount(
                myContext.getString(R.string.sunbeam_ws_url) + "/api/request?type=passengertype&count"
        );
        if(passengerTypeCount == null || passengerTypeCount.Code != 0) //Error
        {
            return -1;
        }
        Log.d(TAG, "[getTotalDataCount] Total: " + passengerTypeCount.Data.get(0).Count);
        //--
        Log.d(TAG, "[getTotalDataCount] 14. bula.mp3, insufficient-balance.mp3, negative-balance.mp3, tapout.mp3...");
        //--
        Log.d(TAG, "----------getTotalDataCount---------");

        int total = conductorCount.Data.get(0).Count //1
                + memberCount.Data.get(0).Count //2
                + busCount.Data.get(0).Count //3
                + routeCount.Data.get(0).Count //4
                + routeAssignCount.Data.get(0).Count  //5
                + stageCount.Data.get(0).Count //6
                + busStationCount.Data.get(0).Count //7
                + busStopCount.Data.get(0).Count //8
                + expressFareCount.Data.get(0).Count //9
                + stageFareCount.Data.get(0).Count //10
                + busSeatCount.Data.get(0).Count //11
                + busAssignCount.Data.get(0).Count //12
                + passengerTypeCount.Data.get(0).Count //13
                + 4; //bula.mp3, insufficient-balance.mp3, negative-balance.mp3, tapout.mp3

        return total;
    }

    public synchronized int checkDataUpdate(String lastUpdate)
    {
        Log.d(TAG, "[checkDataUpdate] -----------checkDataUpdate--------");
        Log.d(TAG, "[checkDataUpdate] 1. Get conductors count...");
        RESTClient ws = new RESTClient(myContext);
        String url = myContext.getString(R.string.sunbeam_ws_url)
                + "/api/request?type=conductor&count&lastupdate="
                + lastUpdate;
        Log.d(TAG, url);
        //--
        conductorCount = ws.doGetDataCount(url);
        if(conductorCount == null || conductorCount.Code != 0) //Error
        {
            return -1;
        }
        Log.d(TAG, "[checkDataUpdate] Total: " + conductorCount.Data.get(0).Count);
        Log.d(TAG, "[checkDataUpdate] 2. Get members count...");
        memberCount = ws.doGetDataCount(
                myContext.getString(R.string.sunbeam_ws_url)
                + "/api/request?type=member&count&lastupdate="
                + lastUpdate
        );
        if(memberCount == null || memberCount.Code != 0) //Error
        {
            return -1;
        }
        Log.d(TAG, "[checkDataUpdate] Total: " + memberCount.Data.get(0).Count);
        Log.d(TAG, "[checkDataUpdate] 3. Get Bus count...");
        busCount = ws.doGetDataCount(
                myContext.getString(R.string.sunbeam_ws_url)
                + "/api/request?type=bus&count&lastupdate="
                + lastUpdate
        );
        if(busCount == null || busCount.Code != 0) //Error
        {
            return -1;
        }
        Log.d(TAG, "[checkDataUpdate] Total: " + busCount.Data.get(0).Count);
        Log.d(TAG, "[checkDataUpdate] 4. Get Route count...");
        routeCount = ws.doGetDataCount(
                myContext.getString(R.string.sunbeam_ws_url)
                + "/api/request?type=route&count&lastupdate="
                + lastUpdate
        );
        if(routeCount == null || routeCount.Code != 0) //Error
        {
            return -1;
        }
        Log.d(TAG, "[checkDataUpdate] Total: " + routeCount.Data.get(0).Count);
        Log.d(TAG, "[checkDataUpdate] 5. Get RouteAssign count...");
        routeAssignCount = ws.doGetDataCount(
                myContext.getString(R.string.sunbeam_ws_url)
                + "/api/request?type=routeassign&count&lastupdate="
                + lastUpdate
        );
        if(routeAssignCount == null || routeAssignCount.Code != 0) //Error
        {
            return -1;
        }
        Log.d(TAG, "[checkDataUpdate] Total: " + routeAssignCount.Data.get(0).Count);
        Log.d(TAG, "[checkDataUpdate] 6. Get Stage count...");
        stageCount = ws.doGetDataCount(
                myContext.getString(R.string.sunbeam_ws_url)
                + "/api/request?type=stage&count&lastupdate="
                + lastUpdate
        );
        if(stageCount == null || stageCount.Code != 0) //Error
        {
            return -1;
        }
        Log.d(TAG, "[checkDataUpdate] Total: " + stageCount.Data.get(0).Count);
        Log.d(TAG, "[checkDataUpdate] 7. Get BusStation count...");
        busStationCount = ws.doGetDataCount(
                myContext.getString(R.string.sunbeam_ws_url)
                + "/api/request?type=busstation&count&lastupdate="
                + lastUpdate
        );
        if(busStationCount == null || busStationCount.Code != 0) //Error
        {
            return -1;
        }
        Log.d(TAG, "[checkDataUpdate] Total: " + busStationCount.Data.get(0).Count);
        Log.d(TAG, "[checkDataUpdate] 8. Get BusStop count...");
        busStopCount = ws.doGetDataCount(
                myContext.getString(R.string.sunbeam_ws_url)
                + "/api/request?type=busstop&count&lastupdate="
                + lastUpdate
        );
        if(busStopCount == null || busStopCount.Code != 0) //Error
        {
            return -1;
        }
        Log.d(TAG, "[checkDataUpdate] Total: " + busStopCount.Data.get(0).Count);
        Log.d(TAG, "[checkDataUpdate] 9. Get ExpressFare count...");
        expressFareCount = ws.doGetDataCount(
                myContext.getString(R.string.sunbeam_ws_url)
                + "/api/request?type=expressfare&count&lastupdate="
                + lastUpdate
        );
        if(expressFareCount == null || expressFareCount.Code != 0) //Error
        {
            return -1;
        }
        Log.d(TAG, "[checkDataUpdate] Total: " + expressFareCount.Data.get(0).Count);
        Log.d(TAG, "[checkDataUpdate] 10. Get StageFare count...");
        stageFareCount = ws.doGetDataCount(
                myContext.getString(R.string.sunbeam_ws_url)
                + "/api/request?type=stagefare&count&lastupdate="
                + lastUpdate
        );
        if(stageFareCount == null || stageFareCount.Code != 0) //Error
        {
            return -1;
        }
        Log.d(TAG, "[checkDataUpdate] Total: " + stageFareCount.Data.get(0).Count);
        Log.d(TAG, "[checkDataUpdate] 11. Get BusSeat count...");
        busSeatCount = ws.doGetDataCount(
                myContext.getString(R.string.sunbeam_ws_url)
                + "/api/request?type=busseat&count&lastupdate="
                + lastUpdate
        );
        if(busSeatCount == null || busSeatCount.Code != 0) //Error
        {
            return -1;
        }
        Log.d(TAG, "[checkDataUpdate] Total: " + busSeatCount.Data.get(0).Count);
        Log.d(TAG, "[checkDataUpdate] 12. Get BusAssign count...");
        busAssignCount = ws.doGetDataCount(
                myContext.getString(R.string.sunbeam_ws_url)
                        + "/api/request?type=busassign&count&lastupdate="
                        + lastUpdate
        );
        if(busAssignCount == null || busAssignCount.Code != 0) //Error
        {
            return -1;
        }
        Log.d(TAG, "[checkDataUpdate] Total: " + busAssignCount.Data.get(0).Count);
        Log.d(TAG, "[checkDataUpdate] 13. Get PassengerType count...");
        passengerTypeCount = ws.doGetDataCount(
                myContext.getString(R.string.sunbeam_ws_url)
                        + "/api/request?type=passengertype&count&lastupdate="
                        + lastUpdate
        );
        if(passengerTypeCount == null || passengerTypeCount.Code != 0) //Error
        {
            return -1;
        }
        Log.d(TAG, "[checkDataUpdate] Total: " + passengerTypeCount.Data.get(0).Count);
        Log.d(TAG, "[checkDataUpdate] ----------checkDataUpdate---------");

        int total = conductorCount.Data.get(0).Count //1
                + memberCount.Data.get(0).Count //2
                + busCount.Data.get(0).Count //3
                + routeCount.Data.get(0).Count //4
                + routeAssignCount.Data.get(0).Count  //5
                + stageCount.Data.get(0).Count //6
                + busStationCount.Data.get(0).Count //7
                + busStopCount.Data.get(0).Count //8
                + expressFareCount.Data.get(0).Count //9
                + stageFareCount.Data.get(0).Count //10
                + busSeatCount.Data.get(0).Count //11
                + busAssignCount.Data.get(0).Count //12
                + passengerTypeCount.Data.get(0).Count; //13

        return total;
    }

    public synchronized int GetConductorData(ProgressDialog updateDialog, int updateDialogProgressValue, String lastUpdate)
    {
        if(conductorCount == null)
            return 0;

        RESTClient ws = new RESTClient(myContext);
        BusinessDBHandler db = new BusinessDBHandler(myContext);

        Log.i(TAG, "[GetConductorData] Begin get Conductor data...");
        int repeat = conductorCount.Data.get(0).Count / dataPageSize +
                (conductorCount.Data.get(0).Count % dataPageSize > 0?1:0);
        int fromISN = conductorCount.Data.get(0).From;

        for(int i=0;i<repeat;i++)
        {
			Log.i(TAG, String.format("[GetConductorData] race %d/%d", i, repeat));
            String url = myContext.getString(R.string.sunbeam_ws_url) +
                    String.format("/api/request?type=conductor&from=%d&length=%d",
                            fromISN, dataPageSize);
            if(lastUpdate != null && lastUpdate.length() > 0)
                url += String.format("&lastupdate=%s", lastUpdate);
            //---
            RestResultValuesExt<ConductorValue> xx = ws.doGetDataExt(
                    url,
                    ConductorValue.class);
            if(xx == null || xx.Code != 0 ||
                    xx.Count == 0) //error
            {
                return -1;
            }
			Log.i(TAG, String.format("[GetConductorData] Add %d conductors to DB", xx.Count));
            //--- Insert to DB
            for(ConductorValue v: xx.Data)
            {
                try
                {
                    db.addOrUpdateConductor(v);
                }
                catch (Exception ex)
                {
                    Log.e(TAG, "[GetConductorData] ERROR: Add Conductor. " + ex.getMessage());
                }
                updateDialogProgressValue++;
                if(updateDialog != null)
                    updateDialog.setProgress(updateDialogProgressValue);
                //---
                try{Thread.sleep(50L);}catch (InterruptedException ex){}
            }
            //---
            fromISN = xx.To+1;
        }
        Log.i(TAG, "[GetConductorData] End get Conductor data.");

        return updateDialogProgressValue;
    }

    public synchronized int GetMemberData(ProgressDialog updateDialog, int updateDialogProgressValue, String lastUpdate)
    {
        if(memberCount == null)
            return 0;

        RESTClient ws = new RESTClient(myContext);
        BusinessDBHandler db = new BusinessDBHandler(myContext);

        Log.i(TAG, "[GetMemberData] Begin get Member data...");
        int repeat = memberCount.Data.get(0).Count / dataPageSize +
                (memberCount.Data.get(0).Count % dataPageSize > 0?1:0);
        int fromISN = memberCount.Data.get(0).From;

        for(int i=0;i<repeat;i++)
        {
			Log.i(TAG, String.format("[GetMemberData] Race %d/%d", i, repeat));
            String url = myContext.getString(R.string.sunbeam_ws_url) +
                    String.format("/api/request?type=member&from=%d&length=%d",
                            fromISN, dataPageSize);
            if(lastUpdate != null && lastUpdate.length() > 0)
                url += String.format("&lastupdate=%s", lastUpdate);
            //---
            RestResultValuesExt<MemberValue> xx = ws.doGetDataExt(
                    url,
                    MemberValue.class);
            if(xx == null || xx.Code != 0 ||
                    xx.Count == 0) //error
            {
                return -1;
            }
            //--- Insert to DB
			Log.i(TAG, String.format("[GetMemberData] Add %d members to DB", xx.Count));
            for(MemberValue v: xx.Data)
            {
                try
                {
                    Log.e(TAG, "[GetMemberData] Add or Update member: " + v.getFullName());
                    long x = db.addOrUpdateMember(v);
                    Log.e(TAG, String.format("[GetMemberData] memberISN: %d result: %d", v.ISN, x));
                }
                catch (Exception ex)
                {
                    Log.e(TAG, "[GetMemberData] ERROR: Add Member. " + ex.getMessage());
                }
                updateDialogProgressValue++;
                if(updateDialog != null)
                    updateDialog.setProgress(updateDialogProgressValue);
                //---
                try{Thread.sleep(50L);}catch (InterruptedException ex){}
            }
            //---
            fromISN = xx.To+1;
        }
        Log.i(TAG, "[GetMemberData] End get Member data.");

        return updateDialogProgressValue;
    }

    public synchronized int GetBusData(ProgressDialog updateDialog, int updateDialogProgressValue, String lastUpdate)
    {
        if(busCount == null)
            return 0;

        RESTClient ws = new RESTClient(myContext);
        BusinessDBHandler db = new BusinessDBHandler(myContext);

        Log.i(TAG, "[GetBusData] Begin get Bus data...");
        int repeat = busCount.Data.get(0).Count / dataPageSize +
                (busCount.Data.get(0).Count % dataPageSize > 0?1:0);
        int fromISN = busCount.Data.get(0).From;

        for(int i=0;i<repeat;i++)
        {
			Log.i(TAG, String.format("[GetBusData] Race %d/%d", i, repeat));
            String url = myContext.getString(R.string.sunbeam_ws_url) +
                    String.format("/api/request?type=bus&from=%d&length=%d",
                            fromISN, dataPageSize);
            if(lastUpdate != null && lastUpdate.length() > 0)
                url += String.format("&lastupdate=%s", lastUpdate);
            //---
            RestResultValuesExt<BusValue> xx = ws.doGetDataExt(
                    url,
                    BusValue.class);
            if(xx == null || xx.Code != 0 ||
                    xx.Count == 0) //error
            {
                return -1;
            }
            //--- Insert to DB
			Log.i(TAG, String.format("[GetBusData] Add %d buses to DB", xx.Count));
            for(BusValue v: xx.Data)
            {
                try
                {
                    db.addOrUpdateBus(v);
                }
                catch (Exception ex)
                {
                    Log.e(TAG, "[GetBusData] ERROR: Add Bus. " + ex.getMessage());
                }
                updateDialogProgressValue++;
                if(updateDialog != null)
                    updateDialog.setProgress(updateDialogProgressValue);
                //---
                try{Thread.sleep(50L);}catch (InterruptedException ex){}
            }
            //---
            fromISN = xx.To+1;
        }
        Log.i(TAG, "[GetBusData] End get Bus data.");

        return updateDialogProgressValue;
    }

    public synchronized int GetRouteData(ProgressDialog updateDialog, int updateDialogProgressValue, String lastUpdate)
    {
        if(routeCount == null)
            return 0;

        RESTClient ws = new RESTClient(myContext);
        BusinessDBHandler db = new BusinessDBHandler(myContext);

        Log.i(TAG, "[GetRouteData] Begin get Route data...");
        int repeat = routeCount.Data.get(0).Count / dataPageSize +
                (routeCount.Data.get(0).Count % dataPageSize > 0?1:0);
        int fromISN = routeCount.Data.get(0).From;

        for(int i=0;i<repeat;i++)
        {
			Log.i(TAG, String.format("[GetRouteData] Race %d/%d", i, repeat));
            String url = myContext.getString(R.string.sunbeam_ws_url) +
                    String.format("/api/request?type=route&from=%d&length=%d",
                            fromISN, dataPageSize);
            if(lastUpdate != null && lastUpdate.length() > 0)
                url += String.format("&lastupdate=%s", lastUpdate);
            //---
            RestResultValuesExt<RouteValue> xx = ws.doGetDataExt(
                    url,
                    RouteValue.class);
            if(xx == null || xx.Code != 0 ||
                    xx.Count == 0) //error
            {
                return -1;
            }
            //--- Insert to DB
			Log.i(TAG, String.format("[GetRouteData] Add %d routes to DB", xx.Count));
            for(RouteValue v: xx.Data)
            {
                try
                {
                    db.addOrUpdateRoute(v);
                }
                catch (Exception ex)
                {
                    Log.e(TAG, "[GetRouteData] ERROR: Add Route. " + ex.getMessage());
                }
                updateDialogProgressValue++;
                if(updateDialog != null)
                    updateDialog.setProgress(updateDialogProgressValue);
                //---
                try{Thread.sleep(50L);}catch (InterruptedException ex){}
            }
            //---
            fromISN = xx.To+1;
        }
        Log.i(TAG, "[GetRouteData] End get Route data.");

        return updateDialogProgressValue;
    }

    public synchronized int GetRouteAssignData(ProgressDialog updateDialog, int updateDialogProgressValue, String lastUpdate)
    {
        if(routeAssignCount == null)
            return 0;

        RESTClient ws = new RESTClient(myContext);
        BusinessDBHandler db = new BusinessDBHandler(myContext);

        Log.i(TAG, "[GetRouteAssignData] Begin get Route-Bus assignment data...");
        int repeat = routeAssignCount.Data.get(0).Count / dataPageSize +
                (routeAssignCount.Data.get(0).Count % dataPageSize > 0?1:0);
        int fromISN = routeAssignCount.Data.get(0).From;

        for(int i=0;i<repeat;i++)
        {
			Log.i(TAG, String.format("[GetRouteAssignData] Race %d/%d", i, repeat));
            String url = myContext.getString(R.string.sunbeam_ws_url) +
                    String.format("/api/request?type=routeassign&from=%d&length=%d",
                            fromISN, dataPageSize);
            if(lastUpdate != null && lastUpdate.length() > 0)
                url += String.format("&lastupdate=%s", lastUpdate);
            //---
            RestResultValuesExt<RouteAssignValue> xx = ws.doGetDataExt(
                    url,
                    RouteAssignValue.class);
            if(xx == null || xx.Code != 0 ||
                    xx.Count == 0) //error
            {
                return -1;
            }
            //--- Insert to DB
			Log.i(TAG, String.format("[GetRouteAssignData] Add %d assignments to DB", xx.Count));
            for(RouteAssignValue v: xx.Data)
            {
                try
                {
                    db.addOrUpdateRouteBus(v);
                }
                catch (Exception ex)
                {
                    Log.e(TAG, "[GetRouteAssignData] ERROR: Add Route-Bus. " + ex.getMessage());
                }
                updateDialogProgressValue++;
                if(updateDialog != null)
                    updateDialog.setProgress(updateDialogProgressValue);
                //---
                try{Thread.sleep(50L);}catch (InterruptedException ex){}
            }
            //---
            fromISN = xx.To+1;
        }
        Log.i(TAG, "[GetRouteAssignData] End get Route-Bus assignment data.");

        return updateDialogProgressValue;
    }

    public synchronized int GetStageData(ProgressDialog updateDialog, int updateDialogProgressValue, String lastUpdate)
    {
        if(stageCount == null)
            return 0;

        RESTClient ws = new RESTClient(myContext);
        BusinessDBHandler db = new BusinessDBHandler(myContext);

        Log.i(TAG, "[GetStageData] Begin get Stage data...");
        int repeat = stageCount.Data.get(0).Count / dataPageSize +
                (stageCount.Data.get(0).Count % dataPageSize > 0?1:0);
        int fromISN = stageCount.Data.get(0).From;

        for(int i=0;i<repeat;i++)
        {
			Log.i(TAG, String.format("[GetStageData] Race %d/%d", i, repeat));
            String url = myContext.getString(R.string.sunbeam_ws_url) +
                    String.format("/api/request?type=stage&from=%d&length=%d",
                            fromISN, dataPageSize);
            if(lastUpdate != null && lastUpdate.length() > 0)
                url += String.format("&lastupdate=%s", lastUpdate);
            //---
            RestResultValuesExt<StageValue> xx = ws.doGetDataExt(
                    url,
                    StageValue.class);
            if(xx == null || xx.Code != 0 ||
                    xx.Count == 0) //error
            {
                return -1;
            }
            //--- Insert to DB
			Log.i(TAG, String.format("[GetStageData] Add %d stages to DB", xx.Count));
            for(StageValue v: xx.Data)
            {
                try
                {
                    db.addOrUpdateStage(v);
                }
                catch (Exception ex)
                {
                    Log.e(TAG, "[GetStageData] ERROR: Add Stage. " + ex.getMessage());
                }
                updateDialogProgressValue++;
                if(updateDialog != null)
                    updateDialog.setProgress(updateDialogProgressValue);
                //---
                try{Thread.sleep(50L);}catch (InterruptedException ex){}
            }
            //---
            fromISN = xx.To+1;
        }
        Log.i(TAG, "[GetStageData] End get Stage data.");

        return updateDialogProgressValue;
    }

    public synchronized int GetBusStationData(ProgressDialog updateDialog, int updateDialogProgressValue, String lastUpdate)
    {
        if(busStationCount == null)
            return 0;

        RESTClient ws = new RESTClient(myContext);
        BusinessDBHandler db = new BusinessDBHandler(myContext);

        Log.i(TAG, "[GetBusStationData] Begin get BusStation data...");
        int repeat = busStationCount.Data.get(0).Count / dataPageSize +
                (busStationCount.Data.get(0).Count % dataPageSize > 0?1:0);
        int fromISN = busStationCount.Data.get(0).From;

        for(int i=0;i<repeat;i++)
        {
			Log.i(TAG, String.format("[GetBusStationData] Race %d/%d", i, repeat));
            String url = myContext.getString(R.string.sunbeam_ws_url) +
                    String.format("/api/request?type=busstation&from=%d&length=%d",
                            fromISN, dataPageSize);
            if(lastUpdate != null && lastUpdate.length() > 0)
                url += String.format("&lastupdate=%s", lastUpdate);
            //---
            RestResultValuesExt<BusStationValue> xx = ws.doGetDataExt(
                    url,
                    BusStationValue.class);
            if(xx == null || xx.Code != 0 ||
                    xx.Count == 0) //error
            {
                return -1;
            }
            //--- Insert to DB
			Log.i(TAG, String.format("[GetBusStationData] Add %d bus stations to DB", xx.Count));
            for(BusStationValue v: xx.Data)
            {
                try
                {
                    db.addOrUpdateBusStation(v);
                }
                catch (Exception ex)
                {
                    Log.e(TAG, "[GetBusStationData] ERROR: Add BusStation. " + ex.getMessage());
                }
                updateDialogProgressValue++;
                if(updateDialog != null)
                    updateDialog.setProgress(updateDialogProgressValue);
                //---
                try{Thread.sleep(50L);}catch (InterruptedException ex){}
            }
            //---
            fromISN = xx.To+1;
        }
        Log.i(TAG, "[GetBusStationData] End get BusStation data.");

        return updateDialogProgressValue;
    }

    public synchronized int GetBusStopData(ProgressDialog updateDialog, int updateDialogProgressValue, String lastUpdate)
    {
        if(busStopCount == null)
            return 0;

        RESTClient ws = new RESTClient(myContext);
        BusinessDBHandler db = new BusinessDBHandler(myContext);

        Log.i(TAG, "[GetBusStopData] Begin get BusStop data...");
        int repeat = busStopCount.Data.get(0).Count / dataPageSize +
                (busStopCount.Data.get(0).Count % dataPageSize > 0?1:0);
        int fromISN = busStopCount.Data.get(0).From;

        for(int i=0;i<repeat;i++)
        {
			Log.i(TAG, String.format("[GetBusStopData] Race %d/%d", i, repeat));
            String url = myContext.getString(R.string.sunbeam_ws_url) +
                    String.format("/api/request?type=busstop&from=%d&length=%d",
                            fromISN, dataPageSize);
            if(lastUpdate != null && lastUpdate.length() > 0)
                url += String.format("&lastupdate=%s", lastUpdate);
            //---
            RestResultValuesExt<BusStopValue> xx = ws.doGetDataExt(
                    url,
                    BusStopValue.class);
            if(xx == null || xx.Code != 0 ||
                    xx.Count == 0) //error
            {
                return -1;
            }
            //--- Insert to DB
			Log.i(TAG, String.format("[GetBusStopData] Add %d bus stops to DB", xx.Count));
            for(BusStopValue v: xx.Data)
            {
                try
                {
                    db.addOrUpdateBusStop(v);
                }
                catch (Exception ex)
                {
                    Log.e(TAG, "[GetBusStopData] ERROR: Add BusStop. " + ex.getMessage());
                }
                updateDialogProgressValue++;
                if(updateDialog != null)
                    updateDialog.setProgress(updateDialogProgressValue);
                //---
                try{Thread.sleep(50L);}catch (InterruptedException ex){}
            }
            //---
            fromISN = xx.To+1;
        }
        Log.i(TAG, "[GetBusStopData] End get BusStop data.");

        return updateDialogProgressValue;
    }

    public synchronized int GetExpressFareData(ProgressDialog updateDialog, int updateDialogProgressValue, String lastUpdate)
    {
        if(busStopCount == null)
            return 0;

        RESTClient ws = new RESTClient(myContext);
        BusinessDBHandler db = new BusinessDBHandler(myContext);

        Log.i(TAG, "[GetExpressFareData] Begin get ExpressFare data...");
        int repeat = expressFareCount.Data.get(0).Count / dataPageSize +
                (expressFareCount.Data.get(0).Count % dataPageSize > 0?1:0);
        int fromISN = expressFareCount.Data.get(0).From;

        for(int i=0;i<repeat;i++)
        {
			Log.i(TAG, String.format("[GetExpressFareData] Race %d/%d", i, repeat));
            String url = myContext.getString(R.string.sunbeam_ws_url) +
                    String.format("/api/request?type=expressfare&from=%d&length=%d",
                            fromISN, dataPageSize);
            if(lastUpdate != null && lastUpdate.length() > 0)
                url += String.format("&lastupdate=%s", lastUpdate);
            //---
            RestResultValuesExt<ExpressFareValue> xx = ws.doGetDataExt(
                    url,
                    ExpressFareValue.class);
            if(xx == null || xx.Code != 0 ||
                    xx.Count == 0) //error
            {
                return -1;
            }
            //--- Insert to DB
			Log.i(TAG, String.format("[GetExpressFareData] Add %d fares to DB", xx.Count));
            for(ExpressFareValue v: xx.Data)
            {
                try
                {
                    db.addOrUpdateExpressFare(v);
                }
                catch (Exception ex)
                {
                    Log.e(TAG, "[GetExpressFareData] ERROR: Add ExpressFare. " + ex.getMessage());
                }
                updateDialogProgressValue++;
                if(updateDialog != null)
                    updateDialog.setProgress(updateDialogProgressValue);
                //---
                try{Thread.sleep(50L);}catch (InterruptedException ex){}
            }
            //---
            fromISN = xx.To+1;
        }
        Log.i(TAG, "[GetExpressFareData] End get ExpressFare data.");

        return updateDialogProgressValue;
    }

    public synchronized int GetStageFareData(ProgressDialog updateDialog, int updateDialogProgressValue, String lastUpdate)
    {
        if(busStopCount == null)
            return 0;

        RESTClient ws = new RESTClient(myContext);
        BusinessDBHandler db = new BusinessDBHandler(myContext);

        Log.i(TAG, "[GetStageFareData] Begin get StageFare data...");
        int repeat = stageFareCount.Data.get(0).Count / dataPageSize +
                (stageFareCount.Data.get(0).Count % dataPageSize > 0?1:0);
        int fromISN = stageFareCount.Data.get(0).From;

        for(int i=0;i<repeat;i++)
        {
			Log.i(TAG, String.format("[GetStageFareData] Race %d/%d", i, repeat));
            String url = myContext.getString(R.string.sunbeam_ws_url) +
                    String.format("/api/request?type=stagefare&from=%d&length=%d",
                            fromISN, dataPageSize);
            if(lastUpdate != null && lastUpdate.length() > 0)
                url += String.format("&lastupdate=%s", lastUpdate);
            //---
            RestResultValuesExt<StageFareValue> xx = ws.doGetDataExt(
                    url,
                    StageFareValue.class);
            if(xx == null || xx.Code != 0 ||
                    xx.Count == 0) //error
            {
                return -1;
            }
            //--- Insert to DB
			Log.i(TAG, String.format("[GetStageFareData] Add %d fares to DB", xx.Count));
            for(StageFareValue v: xx.Data)
            {
                try
                {
                    db.addOrUpdateStageFare(v);
                }
                catch (Exception ex)
                {
                    Log.e(TAG, "[GetStageFareData] ERROR: Add StageFare. " + ex.getMessage());
                }
                updateDialogProgressValue++;
                if(updateDialog != null)
                    updateDialog.setProgress(updateDialogProgressValue);
                //---
                try{Thread.sleep(50L);}catch (InterruptedException ex){}
            }
            //---
            fromISN = xx.To+1;
        }
        Log.i(TAG, "[GetStageFareData] End get StageFare data.");

        return updateDialogProgressValue;
    }

    public synchronized int GetBusSeatData(ProgressDialog updateDialog, int updateDialogProgressValue, String lastUpdate)
    {
        if(busStopCount == null)
            return -1;

        RESTClient ws = new RESTClient(myContext);
        BusinessDBHandler db = new BusinessDBHandler(myContext);

        Log.i(TAG, "[GetBusSeatData] Begin get BusSeat data...");
        int repeat = busSeatCount.Data.get(0).Count / dataPageSize +
                (busSeatCount.Data.get(0).Count % dataPageSize > 0?1:0);
        int fromISN = busSeatCount.Data.get(0).From;

        for(int i=0;i<repeat;i++)
        {
			Log.i(TAG, String.format("[GetBusSeatData] Race %d/%d", i, repeat));
            String url = myContext.getString(R.string.sunbeam_ws_url) +
                    String.format("/api/request?type=busseat&from=%d&length=%d",
                            fromISN, dataPageSize);
            if(lastUpdate != null && lastUpdate.length() > 0)
                url += String.format("&lastupdate=%s", lastUpdate);
            //---
            RestResultValuesExt<BusSeatValue> xx = ws.doGetDataExt(
                    url,
                    BusSeatValue.class);
            if(xx == null || xx.Code != 0 ||
                    xx.Count == 0) //error
            {
                return -1;
            }
            //--- Insert to DB
			Log.i(TAG, String.format("[GetBusSeatData] Add %d bus seats to DB", xx.Count));
            for(BusSeatValue v: xx.Data)
            {
                try
                {
                    db.addOrUpdateBusSeat(v);
                }
                catch (Exception ex)
                {
                    Log.e(TAG, "[GetBusSeatData] ERROR: Add BusSeat. " + ex.getMessage());
                }
                updateDialogProgressValue++;
                if(updateDialog != null)
                    updateDialog.setProgress(updateDialogProgressValue);
                //---
                try{Thread.sleep(50L);}catch (InterruptedException ex){}
            }
            //---
            fromISN = xx.To+1;
        }
        Log.i(TAG, "[GetBusSeatData] End get BusSeat data.");

        return updateDialogProgressValue;
    }

    public synchronized int GetBusAssignData(ProgressDialog updateDialog, int updateDialogProgressValue, String lastUpdate)
    {
        if(busAssignCount == null)
            return -1;

        RESTClient ws = new RESTClient(myContext);
        BusinessDBHandler db = new BusinessDBHandler(myContext);

        Log.i(TAG, "[GetBusAssignData] Begin get BusAssign data...");
        int repeat = busAssignCount.Data.get(0).Count / dataPageSize +
                (busAssignCount.Data.get(0).Count % dataPageSize > 0?1:0);
        int fromISN = busAssignCount.Data.get(0).From;

        for(int i=0;i<repeat;i++)
        {
			Log.i(TAG, String.format("[GetBusAssignData] Race %d/%d", i, repeat));
            String url = myContext.getString(R.string.sunbeam_ws_url) +
                    String.format("/api/request?type=busassign&from=%d&length=%d",
                            fromISN, dataPageSize);
            if(lastUpdate != null && lastUpdate.length() > 0)
                url += String.format("&lastupdate=%s", lastUpdate);
            //---
            RestResultValuesExt<BusAssignValue> xx = ws.doGetDataExt(
                    url,
                    BusAssignValue.class);
            if(xx == null || xx.Code != 0 ||
                    xx.Count == 0) //error
            {
                return -1;
            }
            //--- Insert to DB
			Log.i(TAG, String.format("[GetBusAssignData] Add %d assignments to DB", xx.Count));
            for(BusAssignValue v: xx.Data)
            {
                try
                {
                    db.addOrUpdateBusAssign(v);
                }
                catch (Exception ex)
                {
                    Log.e(TAG, "[GetBusAssignData] ERROR: Add BusAssign. " + ex.getMessage());
                }
                updateDialogProgressValue++;
                if(updateDialog != null)
                    updateDialog.setProgress(updateDialogProgressValue);
                //---
                try{Thread.sleep(50L);}catch (InterruptedException ex){}
            }
            //---
            fromISN = xx.To+1;
        }
        Log.i(TAG, "[GetBusAssignData] End get BusAssign data.");

        return updateDialogProgressValue;
    }

    public synchronized int GetPassengerTypeData(ProgressDialog updateDialog, int updateDialogProgressValue, String lastUpdate)
    {
        if(passengerTypeCount == null)
            return -1;

        RESTClient ws = new RESTClient(myContext);
        BusinessDBHandler db = new BusinessDBHandler(myContext);

        Log.i(TAG, "[GetPassengerTypeData] Begin get PassengerType data...");
        int repeat = passengerTypeCount.Data.get(0).Count / dataPageSize +
                (passengerTypeCount.Data.get(0).Count % dataPageSize > 0?1:0);
        int fromISN = passengerTypeCount.Data.get(0).From;

        for(int i=0;i<repeat;i++)
        {
			Log.i(TAG, String.format("[GetPassengerTypeData] Race %d/%d", i, repeat));
            String url = myContext.getString(R.string.sunbeam_ws_url) +
                    String.format("/api/request?type=passengertype&from=%d&length=%d",
                            fromISN, dataPageSize);
            if(lastUpdate != null && lastUpdate.length() > 0)
                url += String.format("&lastupdate=%s", lastUpdate);
            //---
            RestResultValuesExt<PassengerTypeValue> xx = ws.doGetDataExt(
                    url,
                    PassengerTypeValue.class);
            if(xx == null || xx.Code != 0 ||
                    xx.Count == 0) //error
            {
                return -1;
            }
            //--- Insert to DB
			Log.i(TAG, String.format("[GetPassengerTypeData] Add %d types to DB", xx.Count));
            for(PassengerTypeValue v: xx.Data)
            {
                try
                {
                    db.addOrUpdatePassengerType(v);
                }
                catch (Exception ex)
                {
                    Log.e(TAG, "[GetPassengerTypeData] ERROR: Add PassengerType. " + ex.getMessage());
                }
                updateDialogProgressValue++;
                if(updateDialog != null)
                    updateDialog.setProgress(updateDialogProgressValue);
                //---
                try{Thread.sleep(50L);}catch (InterruptedException ex){}
            }
            //---
            fromISN = xx.To+1;
        }
        Log.i(TAG, "[GetPassengerTypeData] End get PassengerType data.");

        return updateDialogProgressValue;
    }

    public synchronized String getDefaultCardNumber()
    {
        //doGetStringData
        RESTClient ws = new RESTClient(myContext);

        Log.i(TAG, "[getDefaultCardNumber] Begin get default card number...");
        String url = myContext.getString(R.string.sunbeam_ws_url) +
                "/api/request?defaultcard";

        RestResultValues<String> xx = ws.doGetStringData(url);
        if(xx == null || xx.Code != 0) //error
        {
            Log.i(TAG, "[getDefaultCardNumber] get default card number failed");
            return null;
        }
        String value = null;
        if(xx.Data != null && xx.Data.size() > 0)
        {
            BusinessDBHandler db = new BusinessDBHandler(myContext);
            value = xx.Data.get(0);
            db.addOrUpdateRuntimeValues("defaultCard", value);
        }
		Log.i(TAG, "[getDefaultCardNumber] Done");
        return value;
    }

    public synchronized boolean UpdateTransaction()
    {
		try
		{
			BusinessDBHandler db = new BusinessDBHandler(myContext);
			List<TransactionValue> unsync = db.getUnsyncTransaction();
			if(unsync == null || unsync.size() == 0)
			{
				Log.d(TAG, "[UpdateTransaction] No un-sync data");
				return false;
			}
			RESTClient ws = new RESTClient(myContext);
			String url = myContext.getString(R.string.sunbeam_ws_url) +
					"/api/request?transaction";
			for(TransactionValue xx: unsync)
			{
				String json = "";
				JSONSerializer serializer = new JSONSerializer();
				json = serializer.include("Passengers", "VoucherDiscount").serialize(xx);
				if(json == null)
					continue;

				Log.d(TAG, "[UpdateTransaction] " + json);
				RestResultValues<String> rest = ws.doPostData(url, json);
				if(rest == null || rest.Code != 0)
					Log.e(TAG, "Could not update transaction to server");
				else
				{
					int zz = db.updateTransactionSyncResult(xx.ISN, Integer.parseInt(rest.Data.get(0)));
					Log.d(TAG, String.format("Sync transaction successful. %d / %s / %d",
							xx.ISN, rest.Data.get(0), zz));
				}
				Thread.sleep(300);
			}
		}
		catch (Exception ex)
		{
			Log.e(TAG, "EXCEPTION: " + ex.toString());
			return false;
		}
        return true;
    }

    public synchronized boolean UpdateJournal()
    {
        BusinessDBHandler db = new BusinessDBHandler(myContext);
        List<JournalValue> unsync = db.getUnsyncJournal();
        if(unsync == null || unsync.size() == 0)
        {
            Log.d(TAG, "[UpdateJournal] No un-sync data");
            return false;
        }
        RESTClient ws = new RESTClient(myContext);
        String url = myContext.getString(R.string.sunbeam_ws_url) +
                "/api/request?journal";
        for(JournalValue xx: unsync)
        {
            String json = "";
            JSONSerializer serializer = new JSONSerializer();
            json = serializer.serialize(xx);
            if(json == null)
                continue;

            Log.d(TAG, "[UpdateJournal] " + json);
            RestResultValues<String> rest = ws.doPostData(url, json);
            if(rest == null || rest.Code != 0)
                Log.e(TAG, "[UpdateJournal] Could not update journal to server");
            else
            {
                int zz = db.updateJournalSyncResult(xx.ISN, Integer.parseInt(rest.Data.get(0)));
                Log.d(TAG, String.format("[UpdateJournal] Sync transaction successful. %d / %s / %d",
                        xx.ISN, rest.Data.get(0), zz));
            }
        }

        return true;
    }

    public synchronized boolean getRecordingFiles(ProgressDialog updateDialog, int updateDialogProgressValue)
    {
		try
		{
			if(!getRecordingFile(1, updateDialog, updateDialogProgressValue))
				return false;
			if(!getRecordingFile(2, updateDialog, updateDialogProgressValue))
				return false;
			if(!getRecordingFile(3, updateDialog, updateDialogProgressValue))
				return false;
			if(!getRecordingFile(4, updateDialog, updateDialogProgressValue))
				return false;
		}
		catch (Exception ex)
		{
			Log.e(TAG, "[getRecordingFiles] EXCEPTION: " + ex.toString());
			return false;
		}

        return true;

    }

    public synchronized boolean getRecordingFile(
            int fileType,
            ProgressDialog updateDialog,
            int updateDialogProgressValue)
    {
        BusinessDBHandler db = new BusinessDBHandler(myContext);

        RESTClient ws = new RESTClient(myContext);
        String url = myContext.getString(R.string.sunbeam_ws_url) +
                "/api/request?recording&type=" + fileType;
        String fileName = (fileType == 1?"bula.mp3":
                (fileType == 2?"insufficient-balance.mp3":
                (fileType == 3?"negative-balance.mp3":
                (fileType == 4?"tapout.mp3":
                (fileType == 5?"process-error.mp3":"process-complete.mp3"))))
        );
        Log.i(TAG, "[getRecordingFile] Get file " + fileName);
        RestResultValues<String> xx = ws.doGetStringData(url);
        if(xx == null || xx.Code != 0) //error
        {
            Log.i(TAG, "[getRecordingFile] Could not get file " + fileName);
            return false;
        }
        File mp3Location = new File(Environment.getExternalStorageDirectory() + Declarations.ShortHaul_sdCard_Folder);
        if(!mp3Location.exists())
            mp3Location.mkdir();

        try
        {
            byte[] data = Base64.decode(xx.Data.get(0));
            FileOutputStream file = new FileOutputStream(new File(mp3Location, fileName));
            file.write(data);
            file.close();
        }
        catch (Exception ex)
        {
            Log.i(TAG, String.format("[getRecordingFile] Could not save file %s. %s", fileName, ex.getMessage()));
            return false;
        }
        db.addOrUpdateRuntimeValues(fileName, xx.Message);
        //--
        updateDialogProgressValue++;
        if(updateDialog != null)
            updateDialog.setProgress(updateDialogProgressValue);

        return true;

    }

    public synchronized boolean checkRecordingFileChanged(
            int fileType,
            String currentCheckSum)
    {
		try
		{
			RESTClient ws = new RESTClient(myContext);
			String url = myContext.getString(R.string.sunbeam_ws_url) +
					"/api/request?recordingcheck&type=" + fileType
					+ "&inputCheckSum=" + currentCheckSum;

			String fileName = (fileType == 1?"bula.mp3":
					(fileType == 2?"insufficient-balance.mp3":
			        (fileType == 3?"negative-balance.mp3":
                    (fileType == 4?"tapout.mp3":
                    (fileType == 5?"complete.mp3":"error.mp3"))))
			);
			Log.i(TAG, "[checkRecordingFileChanged] Checking file " + fileName);
			RestResultValues<String> xx = ws.doGetStringData(url);
			if(xx == null || xx.Code < 0) //error
			{
				Log.i(TAG, "[checkRecordingFileChanged] Could not get file " + fileName);
				return false;
			}

			if(xx.Code == 100) //file has been changed
			{
				Log.i(TAG, "[checkRecordingFileChanged] File has been changed. " + fileName);
				return true;
			}
		}
		catch (Exception ex)
		{
			Log.e(TAG, "[checkRecordingFileChanged] EXCEPTION: " + ex.toString());
		}

        return false;

    }

    public synchronized List<DailyCollectionItem> getDailySummaryData(int conductorISN)
    {
		try
		{
			RESTClient ws = new RESTClient(myContext);
			String url = myContext.getString(R.string.sunbeam_ws_url) +
					"/api/request?dailysummary&conductor=" + conductorISN;

			Log.i(TAG, "[getDailySummaryData] Get daily summary of conductor: " + conductorISN);
			RestResultValues<DailyCollectionItem> xx = ws.doGetData(
					url,
					DailyCollectionItem.class
			);

			if(xx == null || xx.Code < 0)
			{
				Log.i(TAG, "[getDailySummaryData] No data found");
				return null;
			}

			return xx.Data;
		}
		catch (Exception ex)
		{
			Log.e(TAG, "[getDailySummaryData] EXCEPTION: " + ex.toString());
		}
		return null;
    }

    public synchronized List<DataDeleted> getDataDeleted()
    {
		try
		{
			RESTClient ws = new RESTClient(myContext);
			String url = myContext.getString(R.string.sunbeam_ws_url) +
					"/api/request?datadel";

			Log.i(TAG, "[getDataDeleted] -----------");
			RestResultValues<DataDeleted> xx = ws.doGetData(
					url,
					DataDeleted.class
			);

			if(xx == null || xx.Code < 0)
			{
				Log.i(TAG, "[getDataDeleted] No data found");
				return null;
			}

			return xx.Data;
		}
		catch (Exception ex)
		{
			Log.e(TAG, "[getDataDeleted] EXCEPTION: " + ex.toString());
		}
		return null;
    }
}
